﻿#include "setting.h"
#include "ui_setting.h"
#include "detection.h"
#include "history.h"
#include "regist.h"

#include <QSqlQuery>
#include <QMessageBox>
#include<QSettings>
#include<QDebug>
#include <QStandardPaths>
#include"titlebarfix.h"

//用户配置路径
//QString config_path="C:/Colorectal_user.ini";
QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
QString init_path=settings.value("path").toString();

QString setting::downloadPath=init_path==nullptr?QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)+"/Colorectal_polyps":init_path;

setting::setting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setting)
{

    ui->setupUi(this);
    //关于自动登录的配置读取
    this->readcfgAutoLogin();

    this->userName=login::Qname;
    ui->lineEdit_username->setText(this->userName);

    //初始化邮箱
    this->getEmail();
    ui->lineEdit_email->setText(this->email);

    QSqlQuery query;
    QString sql_user="select role from user where user_name='"+this->userName+"'";
    query.exec(sql_user);
    query.next();
    QString user_role=query.value(0).toString();

    qDebug()<<"角色："<<user_role;
    //如果是医生，隐藏注册按钮
    if(user_role==QStringLiteral("医生")){
        ui->pushButton_regist->hide();
    }


    ui->lineEdit_path->setText(setting::downloadPath);
    ui->pushButton->hide();
}

setting::~setting()
{
    delete ui;
}

//关于自动登录的配置读取
void setting::readcfgAutoLogin()
{
//    QString config_path="C:/Colorectal_user.ini";
//    QSettings settings(config_path,QSettings::IniFormat);
    QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
//    QSettings settings("user.ini",QSettings::IniFormat);
    bool IsAutoLogin=settings.value("IsAutoLogin").toBool();
    qDebug()<<"IsAutoLogin"<<IsAutoLogin;
    ui->checkBox_autoLogin->setChecked(IsAutoLogin);

}
//关于自动登录的配置设置
void setting::savecfgAutoLogin()
{
//    QString config_path="C:/Colorectal_user.ini";
    QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
//    QSettings settings(config_path,QSettings::IniFormat);
//    QSettings settings("user.ini",QSettings::IniFormat);
    settings.setValue("IsAutoLogin",ui->checkBox_autoLogin->isChecked());
}
//判断是不是管理员
bool setting::isAdmin(QString userName)
{
    return false;
}




//跳转到注册界面
void setting::on_pushButton_regist_clicked()
{
    //this->close();
    titlebarFix *tbarFix=new titlebarFix;
    regist*reg=new regist;
    connect(reg,SIGNAL(sendsignal()),tbarFix,SLOT(close()));
    tbarFix->setAttribute(Qt::WA_DeleteOnClose, true);
    tbarFix->setMainWidget(reg);
    tbarFix->setTitleBarText(QStringLiteral("注册新用户"));
    tbarFix->show();
//    reg->show();
}

//修改密码功能
void setting::on_pushButton_modify_clicked()
{


    QSqlQuery query;

    //修改密码功能
    //    QString name = this->userName;
    QString passwd = ui->lineEdit_password->text();
    QString passwd2 = ui->lineEdit_password2->text();
    QString email = ui->lineEdit_email->text();


    QString update=QString("update user set password='%1' where user_name='%2'").arg(passwd).arg(this->userName);
    if(QString::compare(passwd,passwd2)!=0)
    {
        QMessageBox::warning(NULL,QStringLiteral("密码修改失败"),QStringLiteral("两次密码输入不相同！"));
    }
    else if (passwd==NULL&&passwd2==NULL) {
        //自动登录设置
        this->savecfgAutoLogin();
        //路径修改
//        this->on_pushButton_path_clicked();
        if(isChangePath){
            this->changePath(ui->lineEdit_path->text());
        }
        //修改邮箱
        this->changeEmail();
        //昵称修改
        this->on_pushButton_clicked();
//        QMessageBox::information(NULL,QStringLiteral("修改成功"),QStringLiteral("密码未修改，其他已保存"));
    }
    else if(query.exec(update))
    {
        //自动登录设置
        this->savecfgAutoLogin();
        //路径修改
//        this->on_pushButton_path_clicked();
        if(isChangePath){
            this->changePath(ui->lineEdit_path->text());
        }
        //修改邮箱
        this->changeEmail();
        //昵称修改
        this->on_pushButton_clicked();
        QMessageBox::information(NULL, QStringLiteral("密码修改成功"), QStringLiteral("修改密码成功！"));
    }

}

//更改名称
void setting::on_pushButton_clicked()
{
    //修改名称，需要先获取user_id
    QSqlQuery query;
    QString sqlSelcet="select user_id from user where user.user_name='"+this->userName+"'";
    qDebug()<<"sql->user_id:"<<sqlSelcet;
    bool isDo=query.exec(sqlSelcet);
    //    qDebug()<<"user_id do?="<<isDo;
    query.next();
    QString userId=query.value(0).toString();
    QString userN=ui->lineEdit_username->text();

    if(userN!=NULL){
        if(this->userName!=userN){
            QString updateName=QString("update user set user_name='%1' where user_id='%2'").arg(userN).arg(userId);

            if(query.exec(updateName))
            {
                QMessageBox::information(NULL, QStringLiteral("呢称修改成功"), QStringLiteral("下一次登录生效"));
            }
        }
    }
    else{
        QMessageBox::warning(NULL,QStringLiteral("呢称修改失败"),QStringLiteral("呢称不能为空"));
        ui->lineEdit_username->setText(this->userName);
    }
}

//更改图片下载路径
void setting::on_pushButton_path_clicked()
{
    QString filename = QFileDialog::getExistingDirectory();

    if(filename!=nullptr){
        ui->lineEdit_path->setText(filename);
        this->isChangePath=true;
    }
    else{
//        QString config_path="C:/Colorectal_user.ini";
//        QSettings settings(config_path,QSettings::IniFormat);
//        QSettings settings("user.ini",QSettings::IniFormat);
        QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
        QString temp_path=settings.value("path").toString()==nullptr?setting::downloadPath:settings.value("path").toString();
        ui->lineEdit_path->setText(temp_path);

        this->isChangePath=false;
        QMessageBox::warning(NULL, QStringLiteral("修改失败"), QStringLiteral("请选择路径"));
    }

}
void setting::changePath(QString filename)
{
//    QString config_path="C:/Colorectal_user.ini";
//    QSettings settings(config_path,QSettings::IniFormat);
//    QSettings settings("user.ini",QSettings::IniFormat);
    QSettings settings(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.Colorectal_user.ini",QSettings::IniFormat);
    settings.setValue("path",filename);
    setting::downloadPath=filename;
}
//获取邮箱
void setting::getEmail()
{
    QSqlQuery query;
    QString sql_email="select email from user where user_name='"+this->userName+"'";
    query.exec(sql_email);
    query.next();
    this->email=query.value(0).toString();
}
//修改邮箱
void setting::changeEmail()
{
    QString temp_email=ui->lineEdit_email->text();
    //判断是不是邮箱格式
    QRegExp rx("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    if(!rx.exactMatch(temp_email))//匹配邮箱格式
    {
        QMessageBox::warning(this,QStringLiteral("失败"),QStringLiteral("邮箱账号格式不正确,请重新输入"));
        ui->lineEdit_email->setText(this->email);
    }else
    {
        //修改邮箱
        QSqlQuery query;
        QString sql_email="select user_name from user where email='"+temp_email+"'";


        qDebug()<<"sql_email"<<sql_email;
        query.exec(sql_email);
        qDebug()<<QStringLiteral("找到email个数:")<<query.size();

        if(this->email!=temp_email){
            if(query.size()==0){
                   QString updateEmail=QString("update user set email='%1' where user_name='%2'").arg(temp_email).arg(this->userName);
                    query.exec(updateEmail);
            }
            else {
                QMessageBox::warning(this,QStringLiteral("修改邮箱失败"),QStringLiteral("很抱歉，该邮箱已存在"));
                ui->lineEdit_email->setText(this->email);
            }
        }


    }
}
